package com.woniu.order.util;

import cn.hutool.core.lang.Snowflake;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import org.springframework.stereotype.Component;

@Component
public class Alpay {
    public String transferPay(String orderNum,String money,String identity,String name) throws Exception {
        // 构造请求参数以调用接口
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();

        // 设置转账业务的标题
        model.setOrderTitle("201905代发");

        // 设置描述特定的业务场景
        model.setBizScene("DIRECT_TRANSFER");

        // 设置转账业务请求的扩展参数
//        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");

        // 设置业务备注
        model.setRemark("201905代发");
//        String orderNum = new Snowflake(1, 1).nextIdStr();//押金订单编号
        System.out.println(orderNum);
        System.out.println("================orderNum========================");
        // 设置商家侧唯一订单号
        model.setOutBizNo(orderNum);

        // 设置订单总金额
        model.setTransAmount(money);

        // 设置业务产品码 无密转账固定为TRANS_ACCOUNT_NO_PWD
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");

        // 设置收款方信息
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentity(identity);
        payeeInfo.setIdentityType("ALIPAY_USER_ID");
        payeeInfo.setName(name);
        model.setPayeeInfo(payeeInfo);

        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = config().certificateExecute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
            return "调用成功";

        } else {
            System.out.println("调用失败");
            return "调用失败";

            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }
    public AlipayClient config() throws Exception {
//        System.out.println(appid);
//        System.out.println("===================appid==================");
//        AlipayConfig alipayConfig = new AlipayConfig();
//        //设置网关地址
//        alipayConfig.setServerUrl(serverUrl);
//        //设置应用ID
//        alipayConfig.setAppId(appid);
//        //设置应用私钥
//        alipayConfig.setPrivateKey(privateKey);
//        //设置支付宝公钥
//        alipayConfig.setAlipayPublicKey(alipayPublicKey);
//        //设置请求格式固定值json
//        alipayConfig.setFormat("JSON");
//        //设置字符集
//        alipayConfig.setCharset("UTF-8");
//        //设置签名类型
//        alipayConfig.setSignType("RSA2");
//        //实例化客户端
//        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
//        return alipayClient;

        //<-- 请填写您的应用私钥，例如：MIIEvQIBADANB ... ... -->
        String privateKey  = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCo76xUleKdWv5Uus7rWW5AFlkrhrRsPdxwCzHO9icw0y34i9CeXZ5rKC6NFMRBqgwgR7E+ZXpVLEUomTtOoP/XI5eo8kh4JMXJGJqeW9TbuKB+ODhv9++adcxNe491Wz5dlFS/8N/8+nbT1ZRlu1W9qyJZodCrsz6J04hi6t44nGaaZaLXiVQY/SXgbm+eTTq4ZC/ZSrrN+oKzUtUCieUmCkKgtg/Mvegz31bJgf4rN5L55JX95MOP5RdhZTMjPTsu+69vU5Wwyou8tzpiWaoSZSLcLskllDc84pSzqfRKJjx/R0NW1DefUPT5yAlk7c1afuZZkaFmGEQz9uDXimqjAgMBAAECggEAQqmZAiPHMbrHY533qu02UTnAZCmapp/ejaQNOpLtshmTKWHap7NfoP9RJ0DLlPHKKS7BJVMb6+I1RCj7OzLS4g1cekFpEWfJWGpKETVBkJaRVl1O//gBZ50WSnj4iiHAQWMX8knVyVj4bOz5yw8abWbLfj0ZP7fSdMaxKf9uQz3nrLc1OxiHKcW1M5MihDKSkufxDcunh2Md8FF1UgCSyVBZ0TxhQjcvVqiS71OHKTP8sAqtWIIPIMbIyyZWGA0bKcNE4ohMbXR2BHKjH/YEMIuEGyTGJ08P9NBg52KOKQcs4vt2HKg+Es2oiVO3jqVGIeWJYiazsmEF8IZnrUv44QKBgQDgtYMgoooDx8QC7ssl5eBmSMOkpQvxPzECJn71DSpZixUR4mjz5LPNddnmBtC3w7yfUwYjEMWEgaFYfMEXxQM4ImgWf8/grIqzw6DDWoLCtsYdkF/8ko+4lK5IHpMGfZ6lJLIAafkE/HrPovAdmnt9Q0p8Sg2taxnmJ4PLJonzEwKBgQDAdfT4jq9yB9KkSE0JUtmNu2tTpKbzvMCFPOsaBZvXuvXK/q7O2Jo4OSge9ycrDkSyLdbXtPhyDrDvzhwFmydH3mmytvqijqjMgsHzbSe1WiOkXiRfqfER9N6L1v45rfK1wJl3tLcPFoogoyck/nU8Oz2blrnf9C0J0mXgmwoMMQKBgBA7TFOJKisHvRLxKN998W2yB9/WxDwS8nlTSoxs/Qgm0cjO8AHeHVbUEn9ZMq5zhyaDGshhp8LgkkUQDeqnoonlgltNtDhofkRp9yzlVti11y2/hg96jsMoikZPXxNy84i+U9VP0lsX2c8x1+JrjEwKFlFQEBjcyawkYpLEH+bdAoGAVc6BaKwc9qccN9hwWD7S9gjgYgtfz/2QjBbL7u8fO0CEw3tnLpuNrpF1HNEualjTMemUBtoNhrScGF55SdqR2c4ErAQ+nGkkQwHtPP2mnYlRqL+8UdZvv4TAD/LLp6WxN21mPuOYVibDcKetFSZJ2I/E+PBrcLjpt55Rg6E8CpECgYAkrZUmfeh8Ub2ZvR2T6kWqvz4cOVqHtV/dlJSib5wXy/JqPgPzWNHNULj6wLRvsuc0zeglMi5vu/Yq3vTEiWS0QkN1QGNQU2IC5s18nSOhGmWzXsGgjjiWU4sDSUoUNNogMqS6LXB7YOZIIrOaDUrgLLQgBuwphfKcO91Nyyyw8A==";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
//        <-- 请填写您的AppId，例如：2019091767145019 -->
        alipayConfig.setAppId("9021000135693421");
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setFormat("json");
//        <-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->
        alipayConfig.setAppCertPath("D:/woniuxueyuan/java/alipay/appPublicCert.crt");
//        <-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->
        alipayConfig.setAlipayPublicCertPath("D:/woniuxueyuan/java/alipay/alipayPublicCert.crt");
//        <-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->
        alipayConfig.setRootCertPath("D:/woniuxueyuan/java/alipay/alipayRootCert.crt");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        return alipayClient;
    }

}
